#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "nnie_service.h"
#include "hw_app_config.h"
#include "hw_app_log.h"
#include "opencv_api.h"
using namespace HWApp;

// SAMPLE_SVP_NNIE_MODEL_S                 s_stYoloModel;
// SAMPLE_SVP_NNIE_PARAM_S                 s_stYoloNnieParam;
// SAMPLE_SVP_NNIE_YOLOV3_SOFTWARE_PARAM_S s_stYolov3SoftwareParam;
// SAMPLE_SVP_NNIE_CFG_S                   stNnieCfg_SDC;

int32_t FreeYuvImage(ImageInfo &pstFrame)
{
    if (pstFrame.data) {
        free((void *)(uintptr_t)pstFrame.data);
    }
    return 0;
}

void DoIt(std::string modelPath, std::string imagePath)
{
    printf("modelPath = %s\n", modelPath.c_str());
    printf("imagePath = %s\n", imagePath.c_str());
    int ret;
    NNIE nn;
    nn.Init();
    NnCreateParam param = {};
    param.batchNum = 1;
    param.loadModeModel= 1;
    param.modelPath = (char *)modelPath.c_str();
    ret = nn.LoadModel(&param);
    LOG_IF_FAIL(ret == HW_OK, "load model error");
    nn.ParamInit();

    ImageInfo img;
    NNTensor* outTenser;
    std::vector<ObjBox> objBoxs;
    nn.InferenceFile(imagePath.c_str(), img, objBoxs);
    
    drawObjbox(imagePath.c_str(), objBoxs);
    FreeYuvImage(img);

    printf("-----------------------------------------------------\n");

}

int main(int argc, char* argv[])
{
    const char* imagePath = "./jpeg/plate720X1160.jpg";
    if (argc > 1) {
        imagePath = argv[1];
    }
    // std::string modelPath = "./model//e/Wangyn/hw";
    std::string modelPath = "./model/test0610_1_yuv_rgb.wk";
    // std::string modelPath = "./model/yolov5_cx_351X_yuv_rgb_80class_inst.wk";
    if (argc > 2) {
        modelPath = argv[2];
    }
    // DoIt(modelPath, imagePath);

    for (int i = 0; i < 1; i++)
    {
        DoIt(modelPath, "./jpeg/640X640_" + std::to_string(i) + ".jpg");
    }

    for (int i = 0; i < 1; i++)
    {
        DoIt(modelPath, "./jpeg/plate640X640_" + std::to_string(i) + ".jpg");
    }

    // for (int i = 0; i < 4; i++)
    // {
    //     DoIt(modelPath, "./jpeg/plate720X1160_" + std::to_string(i) + ".jpg");
    // }

	return 0;
}